package com.hbuzzs.elasticsearch.controller;

import com.google.common.collect.Lists;
import com.hbuzzs.elasticsearch.dao.TestBeanDaoRepository;
import com.hbuzzs.elasticsearch.entity.MyRequest;
import com.hbuzzs.elasticsearch.entity.TestBean;
import com.hbuzzs.elasticsearch.service.TestService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author zhangzhishun
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class TestController {

    private final TestService testService;
    private final TestBeanDaoRepository testBeanDaoRepository;
    private final ElasticsearchRestTemplate elasticsearchRestTemplate;

    @PostMapping(value = "/save_automatic")
    public Object saveAutomatic(Integer n) {
        int sentimentBjSize;
        String indexName = "index_test";
        if (elasticsearchRestTemplate.indexExists(indexName)) {
            sentimentBjSize = Lists.newArrayList(testBeanDaoRepository.findAll()).size();
        } else {
            log.info("索引不存在，开始创建{}索引", indexName);
            sentimentBjSize = 0;
            boolean index = elasticsearchRestTemplate.createIndex(TestBean.class);
            if (index) {
                elasticsearchRestTemplate.putMapping(TestBean.class);
            } else {
                log.error("索引创建失败");
            }
        }
        for (int i = 1; i < n + 1; i++) {
            log.info("目前数据量：{} 插入第{}个数据", sentimentBjSize + i, i);
            testBeanDaoRepository.save(TestBean.builder()
                    .id((long) (sentimentBjSize + i))
                    .age(20)
                    .desc("desc描述内容")
                    .name("name张三")
                    .sex("男")
                    .insertTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant())).build());
        }
        return true;
    }

    @PostMapping(value = "/save")
    public Object save(@RequestBody TestBean request) {
        return testService.save(request);
    }

    @PostMapping(value = "/delete")
    public Object delete(@RequestBody TestBean request) {
        testService.delete(request);
        return "true";
    }

    @PostMapping(value = "/update")
    public Object update(@RequestBody TestBean request) {
        return testService.update(request);
    }

    @PostMapping(value = "/findAll")
    public Object findAll() {
        return testService.findAll();
    }

    @PostMapping(value = "/find_by_page")
    public Object findFirstPage(@RequestBody MyRequest request) {
        return testService.findFirstPage(request.getStartPage(), request.getN());
    }

    @PostMapping(value = "/findByName")
    public Object findByName(@RequestBody TestBean request) {
        return testService.findByName(request.getName());
    }

    @PostMapping(value = "/findByNameFuzzyQuery")
    public Object findByNameFuzzyQuery(@RequestBody TestBean request) {
        return testService.findByNameFuzzyQuery(request.getName());
    }

    @PostMapping(value = "/findManByNameOrDesc")
    public Object findManByNameOrDesc(@RequestBody TestBean request) {
        return testService.findManByNameOrDesc(request.getName(), request.getDesc());
    }


}
